﻿using System;
using System.Threading.Tasks;
using System.Windows.Forms;
using xxpl1.GAE;
using xxpl1.Log;
using xxpl1.Utils;
using HttpListener = xxpl1.Proxy.HttpListener;

namespace ProxyUI
{
    public partial class MainForm : Form
    {
        private static readonly ILog logger = LogManager.GetLogger("Proxy");
        private HttpListener listener;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_OnLoad(object sender, EventArgs e)
        {
            ((FormAppender)logger).AppendTextBox(textBox1);

            BindSetting();

            StartService();
        }

        private void StartService()
        {
            Task.Factory.StartNew(DnsHelper.TestAccessbility);

            listener = new HttpListener(Settings.Instance.Port);

            listener.Start();

            logger.InfoFormat("Proxy Started at port: " + Settings.Instance.Port);
        }

        private void StatiticsTimer_Tick(object sender, EventArgs e)
        {
            BindStatitics();
        }

        private void BindSetting()
        {
            CacheCheckBox.Checked = Settings.Instance.EnableCache;
            RangeCheckBox.Checked = Settings.Instance.EnableRange;
        }

        private void BindStatitics()
        {
            DownLoadnLabel.Text = string.Format("Download = {0:F2}ms", Statistic.AvgDownloadTime);
            ParsingLabel.Text = string.Format("Parsing = {0:F2}ms", Statistic.AvgParsingTime);
            CacheLabel.Text = Statistic.CacheHit;

            SwitchButton.Text = listener.IsRunning ? "Stop" : "Start";
        }

        private void SwitchButton_Click(object sender, EventArgs e)
        {
            if (listener.IsRunning) listener.Stop();
            else
            {
                Task.Factory.StartNew(DnsHelper.TestAccessbility);
                listener.Start();
            }
        }

        private void RangeCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            Settings.Instance.EnableRange = RangeCheckBox.Checked;
        }

        private void CacheCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            Settings.Instance.EnableCache = CacheCheckBox.Checked;
        }

        private void HostButton_Click(object sender, EventArgs e)
        {
            Task.Factory.StartNew(DnsHelper.TestAccessbility);
        }

        private void NotifyIconMouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
            {
                Visible = true;
                ShowInTaskbar = true;
                WindowState = FormWindowState.Normal;
            }
            else
            {
                Visible = false;
                ShowInTaskbar = false;
                WindowState = FormWindowState.Minimized;
            }
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                Visible = false;
                ShowInTaskbar = false;
                WindowState = FormWindowState.Minimized;
                e.Cancel = true;
                return;
            }

            base.OnFormClosing(e);
        }

        private void ExitToolStripMenuItemClick(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void MainForm_VisibleChanged(object sender, EventArgs e)
        {
            ((FormAppender)logger).TurnOffLog(Visible);
        }
    }
}
